package com.aier.user.service.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.aier.common.dao.ICommonDAO;
import com.aier.user.entity.mapper.UserRowMapper;
import com.aier.user.service.IMyFansService;

@Component("myFansService")
public class MyFansServiceImpl_J implements IMyFansService {

    @Resource(name = "commonDao")
    private ICommonDAO commonDao;

    public void setCommonDao(ICommonDAO commonDao) {
        this.commonDao = commonDao;
    }

    @Override
    public Map<String, Object> searchFansList(int page, int page_size, Long uid, String sort, Short state) {
        String sql = "select u.*,f.state, (select count(*) from follow where uid = u.uid) as cfollow, " +
                "(select count(*) from follow where fuid = u.uid) as cfans, (select count(*) from fresh where uid = u.uid) as cfresh " +
                "from `user` u left join follow f on f.uid = u.uid where f.fuid = ?";
        if (null != state && 0 != state) {
            sql += " and f.state = "+state;
        } 
        if ("cfans".equals(sort)) {
            sql += " order by cfans desc";
        } else {
            sql += " order by f.cdate desc";
        }
        
        return commonDao.queryPage(sql, page, page_size, new Object[]{uid}, new UserRowMapper(UserRowMapper.FANS));
    }

    @Override
    public int deleteFans(Long uid, Long fuid,Short state) {
        
        commonDao.update("delete from follow where uid =? and fuid = ?",new Object[]{fuid,uid});
        // 若为互相关注，修改状态
        if (state == 2) {
            
            commonDao.update("update follow set state = 1 where uid = ? and fuid = ?",new Object[]{uid,fuid});
        }
        return 1;
    }

    

}
